#pragma once

#include <atomic>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <stdio.h>

#include "mqtt/async_client.h"
#include "json.hpp"
#include "yj_time.hpp"
#include "yj_return.hpp"
#include "yj_buffer.hpp"
#include "config.h"

int test(int argc, char *argv[]);

class yj_mqtt
{
private:
    std::string _server_url;
    std::string _client_id;
    std::string _username;
    std::string _password;

    std::vector<std::string> _subscribe_topics;

    int _qos;

    mqtt::async_client::ptr_t _cli;
    mqtt::connect_options   _conn_opts;

    yj_buffer<std::string> _recv_buffer;
    yj_return _init_flag;
public:
    yj_mqtt(std::string server_url,
            std::string client_id,
            std::string username,
            std::string password,
            int qos);

    ~yj_mqtt();

    yj_return init();
    yj_return add_subscribe(std::string topic);
    yj_return publish(std::string topic, std::string message,long timeout_ms = 10000);
    yj_return start(); // 放到一个线程中执行

    yj_return get_recv_vector(std::vector<std::string> &recv_vector);



    void print_config();
};
